package DataProcess;

import baseAPI.AlertStage;
import baseAPI.Database;
import baseClass.History;
import baseClass.HistoryTable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Pane;

import javax.xml.soap.Text;

public class HistoryPage {
    private static Database database;
    private static Pane pane;
    private static CheckBox checkBoxId, checkBoxRoom, checkBoxIn, checkBoxOut;
    private static TextField idText, roomNumberText;
    private static DatePicker calendarIn, calendarOut;
    private static Button btnSearch;
    private static TableView<HistoryTable> tableView = new TableView<>();
    private static TableColumn<HistoryTable, String> tcName, tcAge, tcSex, tcIdNumber, tcNumberOfPeople, tcRoomNumber, tcDateCheckIn, tcDateCheckOut;

    public static void bind(Parent history, Database database){
        HistoryPage.database = database;//数据库初始化
        //控件绑定
        pane = (Pane) history.lookup("#pane");
        checkBoxId = (CheckBox) history.lookup("#checkBoxId");
        checkBoxRoom = (CheckBox) history.lookup("#checkBoxRoom");
        checkBoxIn = (CheckBox) history.lookup("#checkBoxIn");
        checkBoxOut = (CheckBox) history.lookup("#checkBoxOut");
        idText = (TextField) history.lookup("#idText");
        roomNumberText = (TextField) history.lookup("#roomNumberText");
        calendarIn = (DatePicker) history.lookup("#calendarIn");
        calendarOut = (DatePicker) history.lookup("#calendarOut");
        btnSearch = (Button) history.lookup("#btnSearch");

        //表格属性信息
        pane.getChildren().add(tableView);//添加控件
        tableView.setEditable(false);//不可编辑
        tableView.setPrefHeight(645);
        tableView.setPrefWidth(1017);
        tableView.setLayoutX(56);
        tableView.setLayoutY(157);

        tcName = new TableColumn<>("姓名");
        tcAge = new TableColumn<>("年龄");
        tcSex = new TableColumn<>("性别");
        tcIdNumber = new TableColumn<>("身份证号");
        tcNumberOfPeople = new TableColumn<>("入住人数");
        tcRoomNumber = new TableColumn<>("入住房间");
        tcDateCheckIn = new TableColumn<>("入住日期");
        tcDateCheckOut = new TableColumn<>("退房日期");

        tcName.setCellValueFactory(new PropertyValueFactory<HistoryTable, String>("name"));
        tcAge.setCellValueFactory(new PropertyValueFactory<HistoryTable, String>("age"));
        tcSex.setCellValueFactory(new PropertyValueFactory<HistoryTable, String>("sex"));
        tcIdNumber.setCellValueFactory(new PropertyValueFactory<HistoryTable, String>("idNumber"));
        tcNumberOfPeople.setCellValueFactory(new PropertyValueFactory<HistoryTable, String>("numberOfPeople"));
        tcRoomNumber.setCellValueFactory(new PropertyValueFactory<HistoryTable, String>("roomNumber"));
        tcDateCheckIn.setCellValueFactory(new PropertyValueFactory<HistoryTable, String>("dateCheckIn"));
        tcDateCheckOut.setCellValueFactory(new PropertyValueFactory<HistoryTable, String>("dateCheckOut"));

        //添加列
        tableView.getColumns().addAll(tcName, tcAge, tcSex, tcIdNumber, tcNumberOfPeople, tcRoomNumber, tcDateCheckIn, tcDateCheckOut);

        btnSearch.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                inquireHistory();
            }
        });
    }

    public static void updateHistory(Parent history){
        inquireHistory();
    }

    public static void inquireHistory(){
        ObservableList<HistoryTable> histories = FXCollections.observableArrayList();

        if(!checkBoxId.isSelected() && !checkBoxRoom.isSelected() && !checkBoxIn.isSelected() && !checkBoxOut.isSelected()){
            if(History.getHistories(database) == null){
                histories = null;
            }
            else{
                histories.addAll(HistoryTable.getHistoryTable(History.getHistories(database)));
            }
        }
        else{
            String where = "";
            if(checkBoxId.isSelected()){
                if(idText.getText().isEmpty()){
                    AlertStage.show("提示", "请输入身份证号码！");
                    return;
                }
                else if(!idNumRight(idText.getText()))
                {
                    AlertStage.show("提示","请输入正确的身份证号码！");
                }
                where += ("idNumber = " + "\"" + idText.getText() + "\"");
            }

            if(checkBoxRoom.isSelected()){
                if(roomNumberText.getText().isEmpty()){
                    AlertStage.show("提示", "请输入房间号！");
                    return;
                }
                if(!where.isEmpty()){//查询语句非空则需要"and"连接
                    where += " and ";
                }
                where += ("roomNumber = " + "\"" + roomNumberText.getText() + "\"");
            }

            if(checkBoxIn.isSelected()){
                if(calendarIn.getValue() == null){
                    AlertStage.show("提示", "请选择入住日期！");
                    return;
                }
                if(!where.isEmpty()){//查询语句非空则需要"and"连接
                    where += " and ";
                }
                where += ("substring(dateCheckIn, 1, 10) = " + "\"" + calendarIn.getValue().toString() + "\"");
            }

            if(checkBoxOut.isSelected()){
                if(calendarOut.getValue() == null){
                    AlertStage.show("提示", "请选择退房日期！");
                    return;
                }
                if(!where.isEmpty()){//查询语句非空则需要"and"连接
                    where += " and ";
                }
                where += ("substring(dateCheckOut, 1, 10) = " + "\"" + calendarOut.getValue().toString() + "\"");
            }

            if(History.getHistories(database, where) == null){
                histories = null;
            }
            else {
                histories.addAll(HistoryTable.getHistoryTable(History.getHistories(database, where)));
            }
        }

        //更新表格数据
        if(histories == null){
            tableView.getItems().clear();
        }
        tableView.setItems(histories);
        tableView.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
    }

    public static boolean idNumRight(String s) {
        if (s.length() != 18) {
            return false;
        }
        int sum;
        try{
            sum = Integer.parseInt(s.substring(0, 1)) * 7 +
                    Integer.parseInt(s.substring(1, 2)) * 9 +
                    Integer.parseInt(s.substring(2, 3)) * 10 +
                    Integer.parseInt(s.substring(3, 4)) * 5 +
                    Integer.parseInt(s.substring(4, 5)) * 8 +
                    Integer.parseInt(s.substring(5, 6)) * 4 +
                    Integer.parseInt(s.substring(6, 7)) * 2 +
                    Integer.parseInt(s.substring(7, 8)) * 1 +
                    Integer.parseInt(s.substring(8, 9)) * 6 +
                    Integer.parseInt(s.substring(9, 10)) * 3 +
                    Integer.parseInt(s.substring(10, 11)) * 7 +
                    Integer.parseInt(s.substring(11, 12)) * 9 +
                    Integer.parseInt(s.substring(12, 13)) * 10 +
                    Integer.parseInt(s.substring(13, 14)) * 5 +
                    Integer.parseInt(s.substring(14, 15)) * 8 +
                    Integer.parseInt(s.substring(15, 16)) * 4 +
                    Integer.parseInt(s.substring(16, 17)) * 2;}
        catch (NumberFormatException ex)
        {
            return false;
        }
        sum = sum % 11;
        char finalNum = s.charAt(17);
        switch (sum) {
            case 0:
                if (finalNum == '1') return true;
                else return false;
            case 1:
                if (finalNum == '0') return true;
                else return false;
            case 2:
                if (finalNum == 'X') return true;
                else return false;
            case 3:
                if (finalNum == '9') return true;
                else return false;
            case 4:
                if (finalNum == '8') return true;
                else return false;
            case 5:
                if (finalNum == '7') return true;
                else return false;
            case 6:
                if (finalNum == '6') return true;
                else return false;
            case 7:
                if (finalNum == '5') return true;
                else return false;
            case 8:
                if (finalNum == '4') return true;
                else return false;
            case 9:
                if (finalNum == '3') return true;
                else return false;
            case 10:
                if (finalNum == '2') return true;
                else return false;
        }
        return true;
    }

}
